﻿using Hx.ADSyncPlatform.ActiveDirectory.Attributes;
using Hx.ADSyncPlatform.ActiveDirectory.Enums;
using System;
using System.Linq;
using System.Text;

namespace Hx.ADSyncPlatform.ActiveDirectory.Model
{
    /// <summary>
    /// 人员
    /// </summary>
    public class Person : DirectoryObject
    {


        private string samAccountName;
        /// <summary>
        /// 用户登录名（以前版本）
        /// </summary>
        [IgnoreUpdate]
        [RequiredForCreate]
        public string SamAccountName
        {
            get
            {
                return samAccountName;
            }
            set
            {
                samAccountName = value;
                AssignedAttributes.Add(nameof(SamAccountName));
            }
        }

        private DateTime accountExpires;
        /// <summary>
        /// 过期时间
        /// </summary> 
        public DateTime AccountExpires
        {
            get
            {
                return accountExpires;
            }
            set
            {
                accountExpires = value;
                AssignedAttributes.Add(nameof(AccountExpires));
            }
        }

        private string c;
        /// <summary>
        /// 国家，中国：CN，英国：GB
        /// </summary>
        public string C
        {
            get
            {
                return c;
            }
            set
            {
                c = value;
                AssignedAttributes.Add(nameof(C));
            }
        }

        private string company;
        /// <summary>
        /// 公司名称
        /// </summary>
        public string Company
        {
            get
            {
                return company;
            }
            set
            {
                company = value;
                AssignedAttributes.Add(nameof(Company));
            }
        }

        private string department;
        /// <summary>
        /// 部门名称
        /// </summary>
        public string Department
        {
            get
            {
                return department;
            }
            set
            {
                department = value;
                AssignedAttributes.Add(nameof(Department));
            }
        }

        private string departmentNumber;
        /// <summary>
        /// 部门编码
        /// </summary>
        public string DepartmentNumber
        {
            get
            {
                return departmentNumber;
            }
            set
            {
                departmentNumber = value;
                AssignedAttributes.Add(nameof(DepartmentNumber));
            }
        }

        private string employeeId;
        /// <summary>
        /// 员工号
        /// </summary>
        public string EmployeeId
        {
            get
            {
                return employeeId;
            }
            set
            {
                employeeId = value;
                AssignedAttributes.Add(nameof(EmployeeId));
            }
        }

        private string employeeNumber;
        /// <summary>
        /// 员工编号
        /// </summary>
        public string EmployeeNumber
        {
            get
            {
                return employeeNumber;
            }
            set
            {
                employeeNumber = value;
                AssignedAttributes.Add(nameof(EmployeeNumber));
            }
        }

        private string employeeType;
        /// <summary>
        /// 员工类型
        /// </summary>
        public string EmployeeType
        {
            get
            {
                return employeeType;
            }
            set
            {
                employeeType = value;
                AssignedAttributes.Add(nameof(EmployeeType));
            }
        }

        private string facsimileTelephoneNumber;
        /// <summary>
        /// 传真
        /// </summary>
        public string FacsimileTelephoneNumber
        {
            get
            {
                return facsimileTelephoneNumber;
            }
            set
            {
                facsimileTelephoneNumber = value;
                AssignedAttributes.Add(nameof(FacsimileTelephoneNumber));
            }
        }

        private string otherFacsimileTelephoneNumber;
        /// <summary>
        /// 其他传真
        /// </summary>
        public string OtherFacsimileTelephoneNumber
        {
            get
            {
                return otherFacsimileTelephoneNumber;
            }
            set
            {
                otherFacsimileTelephoneNumber = value;
                AssignedAttributes.Add(nameof(OtherFacsimileTelephoneNumber));
            }
        }

        private string givenName;
        /// <summary>
        /// 名
        /// </summary>
        public string GivenName
        {
            get
            {
                return givenName;
            }
            set
            {
                givenName = value;
                AssignedAttributes.Add(nameof(GivenName));
            }
        }

        private string homeMDB;
        /// <summary>
        /// 邮箱标识
        /// </summary>
        public string HomeMDB
        {
            get
            {
                return homeMDB;
            }
            set
            {
                homeMDB = value;
                AssignedAttributes.Add(nameof(HomeMDB));
            }
        }

        private string homeMTA;
        /// <summary>
        /// 邮箱标识
        /// </summary>
        public string HomeMTA
        {
            get
            {
                return homeMTA;
            }
            set
            {
                homeMTA = value;
                AssignedAttributes.Add(nameof(HomeMTA));
            }
        }

        private string homePhone;
        /// <summary>
        /// 家庭电话，若是其他，在前面加other
        /// </summary>
        public string HomePhone
        {
            get
            {
                return homePhone;
            }
            set
            {
                homePhone = value;
                AssignedAttributes.Add(nameof(HomePhone));
            }
        }

        private string homePostalAddress;
        /// <summary>
        /// 家庭住址
        /// </summary>
        public string HomePostalAddress
        {
            get
            {
                return homePostalAddress;
            }
            set
            {
                homePostalAddress = value;
                AssignedAttributes.Add(nameof(HomePostalAddress));
            }
        }

        private string info;
        /// <summary>
        /// 注释
        /// </summary>
        public string Info
        {
            get
            {
                return info;
            }
            set
            {
                info = value;
                AssignedAttributes.Add(nameof(Info));
            }
        }

        private string initials;
        /// <summary>
        /// 英文缩写
        /// </summary>
        public string Initials
        {
            get
            {
                return initials;
            }
            set
            {
                initials = value;
                AssignedAttributes.Add(nameof(Initials));
            }
        }

        private string ipPhone;
        /// <summary>
        /// ip电话
        /// </summary>
        public string IpPhone
        {
            get
            {
                return ipPhone;
            }
            set
            {
                ipPhone = value;
                AssignedAttributes.Add(nameof(IpPhone));
            }
        }

        private string otherIpPhone;
        /// <summary>
        /// 其他IP电话
        /// </summary>
        public string OtherIpPhone
        {
            get
            {
                return otherIpPhone;
            }
            set
            {
                otherIpPhone = value;
                AssignedAttributes.Add(nameof(OtherIpPhone));
            }
        }

        private string l;
        /// <summary>
        /// 市\县
        /// </summary>
        public string L
        {
            get
            {
                return l;
            }
            set
            {
                l = value;
                AssignedAttributes.Add(nameof(L));
            }
        }

        private Int64 lastLogon;
        /// <summary>
        /// 最后登录时间
        /// </summary>
        [ReadOnlyCustom]
        public Int64 LastLogon
        {
            get
            {
                return lastLogon;
            }
            set
            {
                lastLogon = value;
                AssignedAttributes.Add(nameof(LastLogon));
            }
        }

        private Int64 lastLogonTimestamp;
        /// <summary>
        /// 最后登录时间
        /// </summary>
        [ReadOnlyCustom]
        public Int64 LastLogonTimestamp
        {
            get
            {
                return lastLogonTimestamp;
            }
            set
            {
                lastLogonTimestamp = value;
                AssignedAttributes.Add(nameof(LastLogonTimestamp));
            }
        }

        private Int64 logonCount;
        /// <summary>
        /// 登录次数
        /// </summary>
        [ReadOnlyCustom]
        public Int64 LogonCount
        {
            get
            {
                return logonCount;
            }
            set
            {
                logonCount = value;
                AssignedAttributes.Add(nameof(LogonCount));
            }
        }

        private Int64 logonHours;
        /// <summary>
        /// 最后登录时间
        /// </summary>
        [ReadOnlyCustom]
        public Int64 LogonHours
        {
            get
            {
                return logonHours;
            }
            set
            {
                logonHours = value;
                AssignedAttributes.Add(nameof(LogonHours));
            }
        }

        private string mail;
        /// <summary>
        /// 邮箱
        /// </summary>
        public string Mail
        {
            get
            {
                return mail;
            }
            set
            {
                mail = value;
                AssignedAttributes.Add(nameof(Mail));
            }
        }

        private string mailNickName;
        /// <summary>
        /// 邮箱前缀
        /// </summary>
        public string MailNickName
        {
            get
            {
                return mailNickName;
            }
            set
            {
                mailNickName = value;
                AssignedAttributes.Add(nameof(MailNickName));
            }
        }

        private string manager;
        /// <summary>
        /// 管理者
        /// </summary>
        public string Manager
        {
            get
            {
                return manager;
            }
            set
            {
                manager = value;
                AssignedAttributes.Add(nameof(Manager));
            }
        }

        private Int32 mDBOverHardQuotaLimit;
        /// <summary>
        /// 达到该限度时发出警告
        /// </summary>
        [ReadOnlyCustom]
        public Int32 MDBOverHardQuotaLimit
        {
            get
            {
                return mDBOverHardQuotaLimit;
            }
            set
            {
                mDBOverHardQuotaLimit = value;
                AssignedAttributes.Add(nameof(MDBOverHardQuotaLimit));
            }
        }

        private Int32 mDBOverQuotaLimit;
        /// <summary>
        /// 达到该限度时禁止发送
        /// </summary>
        [ReadOnlyCustom]
        public Int32 MDBOverQuotaLimit
        {
            get
            {
                return mDBOverQuotaLimit;
            }
            set
            {
                mDBOverQuotaLimit = value;
                AssignedAttributes.Add(nameof(MDBOverQuotaLimit));
            }
        }

        private Int32 mDBStorageQuota;
        /// <summary>
        /// 达到该限度时禁止发送和接收
        /// </summary>
        [ReadOnlyCustom]
        public Int32 MDBStorageQuota
        {
            get
            {
                return mDBStorageQuota;
            }
            set
            {
                mDBStorageQuota = value;
                AssignedAttributes.Add(nameof(MDBStorageQuota));
            }
        }

        private bool mDBUseDefaults;
        /// <summary>
        /// 使用默认的设置
        /// </summary>
        [ReadOnlyCustom]
        public bool MDBUseDefaults
        {
            get
            {
                return mDBUseDefaults;
            }
            set
            {
                mDBUseDefaults = value;
                AssignedAttributes.Add(nameof(MDBUseDefaults));
            }
        }

        private string memberOf;

        [ReadOnlyCustom]
        /// <summary>
        /// 隶属于，用户组的DN不需要使用引号，多个用分号分隔
        /// </summary>
        public string MemberOf
        {
            get
            {
                return memberOf;
            }
            set
            {
                memberOf = value;
                AssignedAttributes.Add(nameof(MemberOf));
            }
        }

        private string middleName;
        /// <summary>
        /// 中间名字
        /// </summary>
        public string MiddleName
        {
            get
            {
                return middleName;
            }
            set
            {
                middleName = value;
                AssignedAttributes.Add(nameof(MiddleName));
            }
        }

        private string mobile;
        /// <summary>
        /// 移动电话，若多个以英文分号分隔
        /// </summary>
        public string Mobile
        {
            get
            {
                return mobile;
            }
            set
            {
                mobile = value;
                AssignedAttributes.Add(nameof(Mobile));
            }
        }

        private string otherMobile;
        /// <summary>
        /// 其他移动电话
        /// </summary>
        public string OtherMobile
        {
            get
            {
                return otherMobile;
            }
            set
            {
                otherMobile = value;
                AssignedAttributes.Add(nameof(OtherMobile));
            }
        }

        private string pager;
        /// <summary>
        /// 寻呼机
        /// </summary>
        public string Pager
        {
            get
            {
                return pager;
            }
            set
            {
                pager = value;
                AssignedAttributes.Add(nameof(Pager));
            }
        }

        private string otherPager;
        /// <summary>
        /// 其它寻呼机
        /// </summary>
        public string OtherPager
        {
            get
            {
                return otherPager;
            }
            set
            {
                otherPager = value;
                AssignedAttributes.Add(nameof(OtherPager));
            }
        }

        private string physicalDeliveryOfficeName;
        /// <summary>
        /// 办公室
        /// </summary>
        public string PhysicalDeliveryOfficeName
        {
            get
            {
                return physicalDeliveryOfficeName;
            }
            set
            {
                physicalDeliveryOfficeName = value;
                AssignedAttributes.Add(nameof(PhysicalDeliveryOfficeName));
            }
        }

        private string postalAddress;
        /// <summary>
        /// 邮政地址
        /// </summary>
        public string PostalAddress
        {
            get
            {
                return postalAddress;
            }
            set
            {
                postalAddress = value;
                AssignedAttributes.Add(nameof(PostalAddress));
            }
        }

        private string postalCode;
        /// <summary>
        /// 邮政编码
        /// </summary>
        public string PostalCode
        {
            get
            {
                return postalCode;
            }
            set
            {
                postalCode = value;
                AssignedAttributes.Add(nameof(PostalCode));
            }
        }

        private string postOfficeBox;
        /// <summary>
        /// 邮政信箱
        /// </summary>
        public string PostOfficeBox
        {
            get
            {
                return postOfficeBox;
            }
            set
            {
                postOfficeBox = value;
                AssignedAttributes.Add(nameof(PostOfficeBox));
            }
        }

        private Int64 pwdLastSet;
        /// <summary>
        /// 密码最后修改时间
        /// </summary>
        [ReadOnlyCustom]
        public Int64 PwdLastSet
        {
            get
            {
                return pwdLastSet;
            }
            set
            {
                pwdLastSet = value;
                AssignedAttributes.Add(nameof(PwdLastSet));
            }
        }

        private string roomNumber;
        /// <summary>
        /// 房间号
        /// </summary>
        public string RoomNumber
        {
            get
            {
                return roomNumber;
            }
            set
            {
                roomNumber = value;
                AssignedAttributes.Add(nameof(RoomNumber));
            }
        }

        private string showInAddressBook;
        /// <summary>
        /// 是否在地址簿显示
        /// </summary>
        [ReadOnlyCustom]
        public string ShowInAddressBook
        {
            get
            {
                return showInAddressBook;
            }
            set
            {
                showInAddressBook = value;
                AssignedAttributes.Add(nameof(ShowInAddressBook));
            }
        }

        private string sn;
        /// <summary>
        /// 中文名，姓
        /// </summary>
        public string Sn
        {
            get
            {
                return sn;
            }
            set
            {
                sn = value;
                AssignedAttributes.Add(nameof(Sn));
            }
        }

        private string st;
        /// <summary>
        /// 省/自治区
        /// </summary>
        public string St
        {
            get
            {
                return st;
            }
            set
            {
                st = value;
                AssignedAttributes.Add(nameof(St));
            }
        }

        private string street;
        /// <summary>
        /// 街道
        /// </summary>
        public string Street
        {
            get
            {
                return street;
            }
            set
            {
                street = value;
                AssignedAttributes.Add(nameof(Street));
            }
        }

        private string streetAddress;
        /// <summary>
        /// 街道
        /// </summary>
        public string StreetAddress
        {
            get
            {
                return streetAddress;
            }
            set
            {
                streetAddress = value;
                AssignedAttributes.Add(nameof(StreetAddress));
            }
        }

        private string telephoneNumber;
        /// <summary>
        /// 电话号码
        /// </summary>
        public string TelephoneNumber
        {
            get
            {
                return telephoneNumber;
            }
            set
            {
                telephoneNumber = value;
                AssignedAttributes.Add(nameof(TelephoneNumber));
            }
        }

        private string otherTelephone;
        /// <summary>
        /// 其它电话号码
        /// </summary>
        public string OtherTelephone
        {
            get
            {
                return otherTelephone;
            }
            set
            {
                otherTelephone = value;
                AssignedAttributes.Add(nameof(OtherTelephone));
            }
        }

        private string telexNumber;
        /// <summary>
        /// 传真号
        /// </summary>
        public string TelexNumber
        {
            get
            {
                return telexNumber;
            }
            set
            {
                telexNumber = value;
                AssignedAttributes.Add(nameof(TelexNumber));
            }
        }

        private string title;
        /// <summary>
        /// 职务
        /// </summary>
        public string Title
        {
            get
            {
                return title;
            }
            set
            {
                title = value;
                AssignedAttributes.Add(nameof(Title));
            }
        }

        private string url;
        /// <summary>
        /// 网页，其它
        /// </summary>
        public string Url
        {
            get
            {
                return url;
            }
            set
            {
                url = value;
                AssignedAttributes.Add(nameof(Url));
            }
        }

        private string userPrincipalName;
        /// <summary>
        /// 用户登录名
        /// </summary>
        public string UserPrincipalName
        {
            get
            {
                return userPrincipalName;
            }
            set
            {
                userPrincipalName = value;
                AssignedAttributes.Add(nameof(UserPrincipalName));
            }
        }

        private string wWWHomePage;
        /// <summary>
        /// 网页,其它网页以英文分号分隔
        /// </summary>
        public string WWWHomePage
        {
            get
            {
                return wWWHomePage;
            }
            set
            {
                wWWHomePage = value;
                AssignedAttributes.Add(nameof(WWWHomePage));
            }
        }

        private string enabled;
        /// <summary>
        /// 是否启用
        /// </summary>
        [ReadOnlyCustom]
        public string Enabled
        {
            get
            {
                return enabled;
            }
            set
            {
                enabled = value;
                AssignedAttributes.Add(nameof(Enabled));
            }
        }

        private string homePage;
        /// <summary>
        /// 主页
        /// </summary>
        public string HomePage
        {
            get
            {
                return homePage;
            }
            set
            {
                homePage = value;
                AssignedAttributes.Add(nameof(HomePage));
            }
        }

        private UserAccountControlValue userAccountControl;
        /// <summary>
        /// 用户控制
        /// </summary>
        public UserAccountControlValue UserAccountControl
        {
            get
            {
                return userAccountControl;
            }
            set
            {
                userAccountControl = value;
                AssignedAttributes.Add(nameof(UserAccountControl));
            }
        }
        //public long BadPasswordTime { get; set; }

        private string userWorkstations;

        /// <summary>
        /// 计算机
        /// </summary>
        public string UserWorkstations
        {
            get
            {
                return userWorkstations;
            }
            set
            {
                userWorkstations = value;
                AssignedAttributes.Add(nameof(UserWorkstations));
            }
        }


        public string OU
        {
            get
            {
                if (!string.IsNullOrEmpty(this.DistinguishedName) && this.DistinguishedName.Contains(","))
                {
                    string[] distinguishedNameArray = this.DistinguishedName.Split(",");
                    return string.Join(",", distinguishedNameArray.Skip(1));
                }
                return "";
            }
        }

        public string ParentOU
        {
            get
            {
                if (!string.IsNullOrEmpty(this.DistinguishedName) && this.DistinguishedName.Contains(","))
                {
                    string[] distinguishedNameArray = this.DistinguishedName.Split(",");
                    return string.Join(",", distinguishedNameArray.Skip(2));

                }
                return "";
            }
        }

        public string FullDepartment
        {
            get
            {
                string fullDepartment = "";
                if (!string.IsNullOrEmpty(this.DistinguishedName) && this.DistinguishedName.Contains(","))
                {
                    string[] array = this.DistinguishedName.Split(",");
                    StringBuilder sb = new StringBuilder();
                    if (array.Length == 1) return this.OU;
                    for (int i = array.Length - 1; i > 0; i--)
                    {
                        string str = array[i];
                        if (str.StartsWith("OU="))
                        {
                            sb.Append(str.Substring(3));
                            if (i != 1)
                            {
                                sb.Append("/");
                            }
                        }

                    }
                    fullDepartment = sb.ToString();
                }
                return fullDepartment;
            }
        }


        public bool Enable
        {
            get
            {
                return ((int)this.UserAccountControl & 0x0002) == 0;
            }
        }
    }
}
